/****************************************************
    Author:            龙之介
    CreatTime:    2020/12/25 23:15:20
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using ZYKTool;
using System.Linq;
using DG.Tweening;


namespace LongZhiJie
{
    public class TapTapPlayerLogic : MonoBehaviour
    {

        public float speed;
        public Stand _Playerstand;


        private bool CanShowChangeGravaty = true;
        private Rigidbody2D rb;
        private Vector2 moveVelocity;
        private bool canY = false;
        private bool canX = true;
        private void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }

        private void Update()
        {
            if (TapTapStaticValue.PlayerAttributes.Is禁锢) return;
            float x = 0;
            float y = 0;
            ///控制人物移动
            if (!Input.GetMouseButton(0)|| TapTapStaticValue.UpdateRandom玩家攻击可以移动)
            {
                if (canX)
                {
                    x = Input.GetAxisRaw("Horizontal");
                }
                if (x == 0 && canY)
                {
                    y = Input.GetAxisRaw("Vertical");
                }
            }
            moveVelocity = new Vector2(x, y) * TapTapStaticValue.PlayerAttributes.Speed;
            if (Input.GetMouseButton(0))
            {
                Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                moveVelocity += (Vector2)(-1*(target - transform.position).normalized)*2;

                if(_Playerstand==Stand.down|| _Playerstand==Stand.up)
                {
                    moveVelocity -= new Vector2(0, moveVelocity.y);
                }
                else
                {
                    moveVelocity -= new Vector2(moveVelocity.x, 0);
                }
                moveVelocity *= TapTapStaticValue.UpdateRandom后座力;
            }



            // Debug.LogError("??");
            //控制切换重力技能
            if (Input.GetKeyDown(KeyCode.Q) && CanShowChangeGravaty)
            {
                CanShowChangeGravaty = false;
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() => { CanShowChangeGravaty = true; }, TapTapStaticValue.PlayerAttributes.重力转换冷却, 1, ZYKTool.Timer.PETimeUnit.Second);
                改变重力();
            }

        }
        private void FixedUpdate()
        {
            rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
        }
        public void 玩家当前站立位置(Stand stand)
        {
            _Playerstand = stand;
            if (CanShowChangeGravaty == false)
            {
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() => { transform.DOKill(); }, 0.1f, 1, ZYKTool.Timer.PETimeUnit.Second);
            }
        }
        public void 改变重力()
        {
            if (tempStand != Stand.none) return;
            transform.DOKill();// Debug.LogError(_Playerstand);
            //Debug.LogError(_Playerstand);
            if(TapTapStaticValue.UpdateRandom重力切换改为闪现)
            {
                switch (_Playerstand)
                {
                    case Stand.down:
                        transform.DOMove(new Vector3(transform.position.x, 48 + transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.up:

                        transform.DOMove(new Vector3(transform.position.x, -48 + transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.left:

                        transform.DOMove(new Vector3(127 + transform.position.x, transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.right:
                        transform.DOMove(new Vector3(-127 + transform.position.x, transform.position.y, transform.position.z), 0.1f);
                        break;
                }
            }
            else
            {
                switch (_Playerstand)
                {
                    case Stand.down:
                        transform.DOMove(new Vector3(transform.position.x, 48 + transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.up:

                        transform.DOMove(new Vector3(transform.position.x, -48 + transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.left:

                        transform.DOMove(new Vector3(127 + transform.position.x, transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.right:
                        transform.DOMove(new Vector3(-127 + transform.position.x, transform.position.y, transform.position.z), 0.5f);
                        break;
                }
            }
        
        }
        public enum Stand
        {
            none,
            up,
            down,
            right,
            left,
        }
        private Stand tempStand = Stand.none;
        private void OnTriggerEnter2D(Collider2D collision)
        {
            
            if (collision.tag == "Up")
            {
                if(!CanShowChangeGravaty)
                {
                    tempStand =Stand.none;
                    _Playerstand = Stand.up;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.up;
                  
                }
                canX = true;
            }
            if (collision.tag == "Left")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.left;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.left;

                }
                canY = true;
            }
            if (collision.tag == "Right")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.right;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.right;

                }
                canY = true;
            }
            if (collision.tag == "Down")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.down;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.down;

                }
                canX = true;
            }
        }
        private void OnTriggerExit2D(Collider2D collision)
        {

            if (collision.tag == "Up")
            {
                canX = false;
                if(_Playerstand==Stand.up)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                tempStand = Stand.none;
            }
            if (collision.tag == "Left")
            {
                if (_Playerstand == Stand.left)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canY = false;
                tempStand = Stand.none;
            }
            if (collision.tag == "Right")
            {
                if (_Playerstand == Stand.right)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canY = false;
                tempStand = Stand.none;
            }
            if (collision.tag == "Down")
            {
                if (_Playerstand == Stand.down)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canX = false;
                tempStand = Stand.none;
            }
        }

        private int _ID中毒 = -1;
        public void Player解除中毒()
        {
            //Debug.LogError(4)

            if (_ID中毒 == -1)
            {

                _ID中毒 = ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(() =>
                {
                    //Debug.LogError(1)

                    TapTapStaticValue.PlayerAttributes.Is中毒 = false;
                    transform.GetOrAddComponent<SpriteRenderer>().color = Color.white;
                }, 4, 1, ZYKTool.Timer.PETimeUnit.Second);
            }
            else
            {

                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerDelectTimeTask(_ID中毒);
                _ID中毒 = ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(() =>
                {
                    //Debug.LogError(2)

                    TapTapStaticValue.PlayerAttributes.Is中毒 = false;
                    transform.GetOrAddComponent<SpriteRenderer>().color = Color.white;
                }, 4, 1, ZYKTool.Timer.PETimeUnit.Second);
            }

        }

    }
}